﻿// ## 2023/11/21 # 铃 # 各项人口名字改为总人口.兵役人口,预备兵 防止概念混乱.由于总人口表达的实际含义是常规人口(非兵役人口)人口,因此重构相应的概念和算法,.##
// ## 2023/3/21 # 铃 # 优化新的兵役人口增长.##
// ## 2023/3/16 # 铃 # 新增灾难流民系统.##
// ## 2023/3/14 # 铃 # 新增小城市的丰收系统,大小城市共享所属地区城市的丰收.疫病属性,但特技不共享..##
// ## 2023/3/11 # 铃 # 新增了战争时人口迁移的函数.##
// ## 2023/3/2 # 铃 # 新增了府的人口设定.##
// ## 2023/2/11 # 铃 # 新增了战争时人口迁移的函数.##
// ## 2023/2/05 # 铃 # 修复了一些问题,1.对人口较低的城市,直接增加了2%的增长,和之前的千分之5差距过大.4.空城低治安的时候人口不应该下降 5.小城市增长太低##
// ## 2022/10/09 # 铃 # 给了两套人口默认值.重新设定了初始人口数值了 ##
// ## 2022/08/21 # 铃 # 删除了预备兵的设定，把人口相关的信息显示移动到954内政自动化中 ##
// ## 2022/03/17 # 江东新风 # 文字往下掉问题 ##
// ## 2022/02/17 # 江东新风 # 人口上限修正 ##
// ## 2022/02/14 # 江东新风 # 部分常量中文化 ##
// ## 2022/02/12 # 江东新风 # 丰收不起效bug，伤兵恢复下限 ##
// ## 2021/10/29 # 江东新风 # 结构体存储调用方式改进 ##
// ## 2021/10/10 # 江东新风 # 人口设定扩展到非城市据点，蝗灾，疫病，丰收，掌握人心的影响 ##
// ## 2021/10/07 # 江东新风 # 基础人口，人口增长，兵役人口人口，预备兵等设定 ##
// ## 2021/01/19 # 江东新风 # 构思 ##
namespace 人口系统
{
	/// ============================================ < 人口自定义项-放到301 > ============================================ ///

	/// ========================================================================================================= ///
	const array<int> 各城市人口 = { 91650, 56550, 334100, 455000, 559000, 383500, 1671150, 364000, 461500, 62400, 282100, 346450, 656500, 385450, 303550, 1741350, 289250, 445250, 250900, 52650, 162500, 199550, 487500, 461500, 479700, 117000, 430300, 253500, 123500, 147550, 500500, 490750, 110500, 118950, 296400, 130000, 136500, 279500, 78000, 975000, 247000, 305500 };
	const bool 调试模式 = false;
	const bool 兵役政策调试模式 = false;
	class Main
	{
		Main()
		{
			pk::bind2(102, pk::trigger102_t(onGameStart), -1);
			pk::bind(106, pk::trigger106_t(onLoad)); // 在读档前
			pk::bind(108, pk::trigger108_t(onNewMonth));
			pk::bind(107, pk::trigger107_t(onNewDay2));

			// 把人口相关的信息显示移动到954内政自动化中 ##
			//  pk::bind(108, pk::trigger108_t(onNewMonth));
			//  pk::bind(120, pk::trigger120_t(func_信息显示_人口信息), 999);//数字越大越优先

			pk::bind2(171, pk::trigger171_t(onUnitRemove), 9);//伤兵转换需要早于信息初始化
			pk::set_func(64, pk::func64_t(City_Level));
		}

		int City_Level(pk::city @city) //
		{
			// 地图的模型数据在结构体之前载入，导致数据跟不上刷新，还有滞后的bug
			int city_id = city.get_id();
			int level = pk::max(0, ch::get_base_p(city_id).city_level - 1);
			// pk::trace("城市" + pk::get_new_base_name(city_id) + "级别" + level);
			return level;
			// int city_id = city.get_id();
			// int population = ch::get_base_p(city_id).population;
			//// pk::trace("城市" + pk::get_new_base_name(city_id) + "人口" + population);
			// if (population > 四级城市阈值)
			//	return 2;
			// else if (population > 三级城市阈值)
			//	return 1;
			// else
			//	return 0;
		}

		// 开局时设定基础人口数据
		void onGameStart()
		{
			if (!开启人口系统)
				return;

			if (pk::get_scenario().loaded)
			{

				// pk::trace("set_interior_land");
				set_interior_land();
			}
			else
			{
				int scen_no = pk::get_scenario().no;
				for (int city_id = 0; city_id < 城市_末; ++city_id)
				{

					auto city_t = ch::get_base_p(city_id);
					city_t.population = int(大城剧本人口[city_id][0] * 大城剧本人口[city_id][scen_no + 1] / 100);
					city_t.mil_pop_all = int(sqrt(大城剧本人口[city_id][0] * 大城剧本人口[city_id][scen_no + 1] / 100) * 30 * difficulty_when_start(city_id) + 5000);

					city_t.city_level = ch::get_city_level(city_id);
					//city_t.city_level = 4;
					ch::set_interior_land(city_id);
					// if (city_id == 城市_襄阳) pk::trace(pk::format("onGameStart,襄阳人口：{}", city_t.mil_pop_all));
				}
				for (int i = 城市_末; i < 据点_末; ++i)
				{
					pk::building @base = pk::get_building(i);
					auto base_t = ch::get_base_p(i);
					int list_id = i - 城市_末;
					base_t.population = int(小城剧本人口[list_id][0] * 小城剧本人口[list_id][scen_no + 1] / 100);
					base_t.mil_pop_all = int(sqrt(小城剧本人口[list_id][0] * 小城剧本人口[list_id][scen_no + 1] / 100) * 30 * difficulty_when_start(list_id)) + (base.facility == 设施_关隘 ? 0 : 2000);
				}

							// 给府设定人口
				for (int i = 0; i < ch::get_spec_end(); ++i)
				{

					pk::point pos = ch::get_spec_pos(i);
					pk::building @building = pk::get_building(pos);
					if (building is null)
						continue;
					// pk::trace(pk::format("onGameStart,地名：{} :{}是否存活:{}", ch::get_spec_name(i),i,pk::is_alive(building)));

					int spec_id = ch::to_spec_id(building.get_id());

					if (spec_id == -1)
						continue;
					// pk::trace(pk::format("onGameStart,地名：{},spec_id :{},i :{}",  ch::get_spec_name(spec_id),spec_id,i));

					int city_id = pk::get_building_id(building.pos);
					auto name = ch::get_spec_name(i);

					auto base_t = ch::get_base_p(pk::get_city_id(building.pos));
					auto spec_p = ch::get_spec_p(spec_id);
					spec_p.population = int(sqrt(base_t.population)) * 50 + pk::rand(10000);
					if (name.findFirst("郡") >= 0 or name.findFirst("国") >= 0)
						spec_p.population = int(spec_p.population + 10000 + spec_p.population * 0.15);
					if (name.findFirst("京口") >= 0 or name.findFirst("石头城") >= 0 or name.findFirst("赤壁") >= 0)
						spec_p.population = int(spec_p.population * 0.3);
					if (spec_id >= ch::get_normal_spec_end())
						spec_p.population = int(spec_p.population + 100000 + spec_p.population * 0.8);
				}
			}
			func_pop_calc(false);
		}

		void onLoad(int file_id, pk::s11reader@ r)
		{
			func_pop_calc(false);
			for (int i = 0; i < 城市_末; ++i)
			{
				pk::city @city = pk::get_city(i);
				for (int j = 14; j < 22; ++j)
				{
					city.dev[j].pos = pk::point(-1, -1);
				}
			}
		}

		void onNewMonth()
		{
			if (!开启人口系统)
				return;
			//pk::building @building = pk::get_building(铃据点_潼关);
			// for (int i = 0; i < 据点_末; i++)
			// {
			// 	pk::building @neighbor_building = pk::get_building(i);
			// 	int distanceToBuilding = pk::get_building_distance(building.get_id(), i, building.get_force_id());
			// 		pk::trace(pk::format("{}到{},距离：{}", pk::decode(pk::get_name(building)), pk::decode(pk::get_name(neighbor_building)), distanceToBuilding));
			// }
			func_pop_calc(true);
		}

		array<float> pop_gain_calc(据点_末, 0);

		void func_pop_calc(bool type)
		{
			// 人口增长
			for (int base_id = 0; base_id < 据点_末; base_id++)
			{
				pk::building @building0 = pk::get_building(base_id);

				// 空城不用考虑所有人口系统;
				if (building0.get_force_id() == -1)
					continue;
				auto base_t = ch::get_base_p(base_id);
				pk::force @force = pk::get_force(building0.get_force_id());
				pk::person @kunshu = pk::get_person(force.kunshu);
				int city_id = pk::get_city_id(building0.pos);
				pk::city @city0 = pk::get_city(city_id);

				auto building_p = ch::get_baseIA(base_id);
				float final_rate = 0;
				bool 疫病状态 = city0.ekibyou;
				bool 蝗灾状态 = city0.inago;

				//*********************************************  人口增长  *************************************************************************

				int public_order = 0;
				if (base_id < 城市_末)
					public_order = pk::get_city(base_id).public_order;
				else
					public_order = base_t.public_order;

				// 重要!!人口修正的意义是为了加快低人口城市的增长,削弱高人口的增长.参数经过拟合和简化,不能随意调整
				//  指数函数和双曲线函数相比,在30万以下提升更明显,因此暂时采用指数倒数函数
				//  float 人口修正= 1/(population_t / 10000 * 0.02 +0.7)-0.3 + 1;
				// 5万人口1.6倍,20万人口1.3倍,50万人口1.1倍,100万人口1.0倍,150万人口0.88倍.250万人口0.7倍
				float 人口修正 = (1 - log(base_t.population / 10000) / 5 + 0.9f);

				// 治安这么设定,是因为回合开始的时候会掉治安.导致增长变慢
				float 治安修正 = pk::min(1.0, (public_order - 95.f) * 0.01 + 1.0f);

				// 君主的加成和难度的加成  大致上在1.2到1.5倍之间
				float 君主修正 = 1.0 + ((kunshu.stat[武将能力_魅力] + kunshu.stat[武将能力_政治]) / 2.0) * 0.006;

				// 拥立献帝的人口加成
				float 献帝_官职加成 = 0;
				if (pk::is_protecting_the_emperor(force))
					献帝_官职加成 = 0.0005;
				else
				{
				// 官职的人口加成,
					献帝_官职加成 = (10.0 - force.title) * 0.0005 * 0.1;
				}

				final_rate = 人口基础增长率 * 人口修正 * 治安修正 * 君主修正 + 献帝_官职加成;

				// 电脑的加成
				final_rate = final_rate * (building0.is_player() ? 玩家征兵倍率 : 电脑征兵倍率) / 100.f;
				final_rate = final_rate * (building0.is_player() ? 100 : inter::domestic_difficulty_impact()) / 100.f;

				// 育民只能增加基础的50%,而不是继续线性增加
				if (ch::has_skill(building0, 特技_育民))
					final_rate += final_rate + (人口基础增长率 * pk::core::skill_constant_value(特技_育民) / 100); // 育民

				if (city_tech::is_building_force_has_city(building0, 40))
					final_rate += final_rate + (人口基础增长率 * 5 / 100);

				// 丰收之前限定增长
				final_rate = pk::min(final_rate, 0.005);

				if (city0.housaku)
				{
					final_rate += 人口基础增长率; // 丰收状态增长率翻倍
					if (city_tech::is_building_force_has_city(building0, 34))
						final_rate += 人口基础增长率 * 2; // 丰收状态增长率翻倍
				}

				//pk::trace(pk::format("城市名：{},final_rate：{},君主修正{},官职加成：{},人口修正：{}", pk::decode(pk::get_name(building0)), final_rate, 君主修正, 献帝_官职加成, 人口修正));

				//*********************************************  AI兵役政策*************************************************************************
				// 总体来说,越考虑长期的势力越休养,越考虑短期的势力越积极
				if (!building0.is_player() and type)
					building_p.building_policy = 2;

				if ((pk::get_taishu_id(building0) != -1) and !building0.is_player() and building0.get_id() >= 0 and building0.get_id() < 87)
				{
					float AI兵役政策计算 = 2.0;

					// 非兵役人口因子
					AI兵役政策计算 += (1.0 - base_t.population / 200.0 / 10000.0); // 人口越多越倾向征兵,越少倾向于育民

					if (building0.get_id() == 铃据点_宛 and 兵役政策调试模式)
						pk::trace(pk::format("1{}：{}", pk::decode(pk::get_name(building0)), AI兵役政策计算));

					// 兵役人口影响
					AI兵役政策计算 += (int(base_t.mil_pop_all) - 30000.0) / 30000.0; // 每3万兵役 ,减一级政策

					// 兵力影响
					AI兵役政策计算 += (int(pk::get_troops(building0)) - 50000.0) / 100000.0; // 每10万兵力人口增加一级兵役政策.


					// 城市人口太少尽量休养

					if (base_t.population < 30 * 10000)
						AI兵役政策计算 += 0.2;

					if (pk::get_gold(building0) < 3000)
						AI兵役政策计算 += 0.2;

					if (pk::get_food(building0) < 30000)
						AI兵役政策计算 += 0.2;


					// 关隘人口不多,更倾向于休养
					if (building0.facility == 设施_关隘)
						AI兵役政策计算 += 0.3;

					// 城市越多越需要长期主义
					AI兵役政策计算 += pk::get_city_list(force).count * 0.05 - 0.1;

					// 根据前线情况调整
					// 战斗据点
					if (ai::base_in_range_enemy_troops[base_id] > 10000)
					{
						if (pk::get_city_list(force).count == 1) // 如果只有一个城市还在战斗,就不需要考虑长期问题了
							AI兵役政策计算 -= 1;
						else
							AI兵役政策计算 -= 0.3;
					}

					//  危险据点
					else if (ch::neibor_enemy_city_count(pk::get_building(base_id), 1) >= 3)
						AI兵役政策计算 -= 0.2;

					// 前线据点
					else if (ch::neibor_enemy_city_count(pk::get_building(base_id), 1) >= 1)
						AI兵役政策计算 -= 0.1;

					// 警戒据点
					else if (ch::neibor_enemy_city_count(pk::get_building(base_id), 1) == 0 and ch::neibor_enemy_city_count(pk::get_building(base_id), 2) > 0)
						AI兵役政策计算 += 0.0;

					// 完全后方据点
					else if (ch::neibor_enemy_city_count(pk::get_building(base_id), 2) == 0)
						AI兵役政策计算 += 0.3;

					// 越胆小冷静越倾向育民,越鲁莽越倾向于积极.
					int character = kunshu.character;
					AI兵役政策计算 += (2 - kunshu.character) * 0.1;

					int 君主画像 = kunshu.stat[武将能力_政治] * 2 + kunshu.stat[武将能力_魅力] - kunshu.stat[武将能力_武力] * 2 - kunshu.stat[武将能力_统率];
					AI兵役政策计算 += 君主画像 * 0.01;

					// 特定君主的政策
					if (kunshu.get_id() == 武将_张鲁)
						AI兵役政策计算 += 0.5;

					if (kunshu.get_id() == 武将_刘备 or kunshu.get_id() == 武将_诸葛亮 or kunshu.get_id() == 武将_刘禅 or kunshu.get_id() == 武将_刘永 or kunshu.get_id() == 武将_刘表)
						AI兵役政策计算 += 0.0;

					if (kunshu.get_id() == 武将_曹操 or kunshu.get_id() == 武将_曹丕 or kunshu.get_id() == 武将_司马懿 or kunshu.get_id() == 武将_袁绍 or kunshu.get_id() == 武将_公孙瓒)
						AI兵役政策计算 -= 0.3;

					if (kunshu.get_id() == 武将_董卓 or kunshu.get_id() == 武将_吕布 or kunshu.get_id() == 武将_李傕 or kunshu.get_id() == 武将_郭汜)
						AI兵役政策计算 -= 1;

					// AI大城尽量不要用休养生息(不征兵)
					if (building0.get_id() < 城市_末)
						AI兵役政策计算 = pk::min(AI兵役政策计算, 3.0);


					AI兵役政策计算 = pk::max(pk::min(AI兵役政策计算, 4.0), 0.0);
					if (type) building_p.building_policy = (int(AI兵役政策计算 + 0.5)); // 四舍五入
				}

				//********************************  兵役政策影响增长率 *******************
				if (building_p.building_policy >= 0)
				{
					//pk::trace(pk::format("判决{},兵役政策：{}", pk::decode(pk::get_name(pk::get_building(base_id))), building_p.building_policy));

					if (building_p.building_policy == 兵役政策_强征)
					{
						final_rate += 人口基础增长率 * -0.5;
						//ch::add_public_order(building0, -10, true);
					//pk::trace(pk::format("强征判决{},兵役政策：{}", pk::decode(pk::get_name(pk::get_building(base_id))), building_p.building_policy));

					}
					if (building_p.building_policy == 兵役政策_积极)
						final_rate += 人口基础增长率 * -0.3;
					if (building_p.building_policy == 兵役政策_均衡)
						final_rate += 人口基础增长率 * 0;
					if (building_p.building_policy == 兵役政策_安民)
						final_rate += 人口基础增长率 * 0.3;
					if (building_p.building_policy == 兵役政策_休养)
						final_rate += 人口基础增长率 * 0.6;
				}
				//*********************************************  人口增长.  *************************************************************************

				// base_t.population由于并不包含兵役人口,因此它的含义为常规人口(非兵役人口)人口.

				int total_pop = base_t.population + base_t.mil_pop_all;
				int pop_gain = pk::max(0, int(total_pop * final_rate));

				//*********************************************.兵役人口增长  *************************************************************************
				// base_t.population由于并不包含兵役人口,因此它的含义为常规人口(非兵役人口)人口.
				// 从现实逻辑来理解 ,兵役人口为成年男性,常规人口(非兵役人口)人口为男女儿童,女性成人和老人.因此兵役人口的增量来自于男性儿童的成人,
				// 假设人口均匀分布,兵役人口的增量应等于常规人口(非兵役人口)人口的1/30,月度需要再/12,也就是约300多人有一人从常规人口(非兵役人口)人口变为兵役人口
				// 人口增长的基数是所有人口项目之和的增长

				// 均衡状态,常规人口(非兵役人口)人口中,每个月380人中有一人成年.
				float 月度转换比例 = 1.0 / 380.0;
				int 上限征兵 = int(pop_gain * 0.9);
				if (!building0.is_player()) 上限征兵 = int(pop_gain * 0.95);
				float 兵役政策系数 = 0;

				if (building_p.building_policy >= 0)
				{
					兵役政策系数 = 1.8 - building_p.building_policy * 0.5; // 1.3,1,15,1.0,0.85
					兵役政策系数 = pk::max(0.0, pk::min(兵役政策系数, 2.0));

					月度转换比例 = 月度转换比例 * 兵役政策系数;

					if (int(building_p.building_policy) == 4) 上限征兵 = 0;
					else if (int(building_p.building_policy) == 3) 上限征兵 = int(上限征兵 * 0.7);
					else if (int(building_p.building_policy) == 2) 上限征兵 += 0;
					else if (int(building_p.building_policy) == 1) 上限征兵 += 1200;     //积极动员的时候小幅增加上限.
					else if (int(building_p.building_policy) == 0) 上限征兵 += 3000;     //强制征兵的时候大幅增加上限.
				}


				// 这里的转换系数非常重要,可以保证在人口增速低时候,依然有人口增长.这样其实增加了人口对征兵的制约.
				// 即使是人口增长只有2‰,复利10年人口依然可以从100万变成150万.
				// 一方面保证了人口对兵力的强制约,一方面保证了人口的正增长,解决了目前遇到的两个最大的问题.

				int mil_gain = int(base_t.population * 月度转换比例 + ((building0.get_id() < 城市_末) ? 200 : 100)); // 为了减少大小城市的征兵差异,增加常数以平衡差距

				if (building_p.building_policy == 4)
					mil_gain = 0;

				mil_gain = int(mil_gain * (building0.is_player() ? 玩家征兵倍率 : 电脑征兵倍率) / 100.f); // 如果兵役不优待的话,征兵倍率就无用了
				//mil_gain = int(mil_gain * (building0.is_player() ? 100 : inter::domestic_difficulty_impact()) / 100.f);

				//电脑才有征兵上限的限制,玩家没有
				if (!building0.is_player()) mil_gain = pk::min(mil_gain, 上限征兵);									  // 注意兵役人口和人口增长的基数不同

				if (type) ch::add_population(base_id, pop_gain - mil_gain);
				if (type) ch::add_mil_pop_all(base_id, mil_gain);
				base_t.mil_pop_av = mil_gain;
				pop_gain_calc[building0.get_id()] = (pop_gain - mil_gain);

				//pk::trace(pk::format("{},人口：{}，月人口增速：{}，月人口增长：{},月兵役增长：{}，兵役政策：{}，月度转换比例：{}，兵役政策系数：{},mil_gain：{},上限征兵：{}", pk::decode(pk::get_name(pk::get_building(base_id))), total_pop, final_rate, pop_gain, mil_gain,building_p.building_policy, 月度转换比例, 兵役政策系数, mil_gain, 上限征兵));

			   //*******************************************  战争流民  *********************************************************************************************
			   // 如果有敌军在领地活动,人口会有部分变为难民
				int enemy_troops = ai::base_in_range_enemy_troops[building0.get_id()];
				if (enemy_troops > 0 and building0.facility != 设施_关隘)
				{
					float diff_rate = (1 + sqrt(enemy_troops) / 100) / 1000;
					int deserter = pk::min(5000, int(base_t.population * diff_rate));
					if (type) ch::add_population(base_id, -deserter);
					int province_id = pk::get_province_id(building0.pos);
					// pk::trace(pk::format("{},流失人口：{}", pk::decode(pk::get_name(pk::get_building(base_id))), deserter));

					if (type) ch::get_set_p(0).province_deserter[province_id] += deserter;

					pop_gain_calc[building0.get_id()] -= deserter;
				}

				//*******************************************  兵临城下流民  *********************************************************************************************
				int has_enemy_type = has_enemy(pk::get_building(base_id));
				// 0无敌军，1：3格内敌军，2:一格内敌军
				float enemy_inf = has_enemy_type == 2 ? -0.02f : (has_enemy_type == 1 ? -0.01f : 0.f);

				if (has_enemy_type != 0)
				{
					// 难民逃散到其他邻近城市去了
					// 损失的部分80%分摊为人口和20%兵役人口
					if (type) ch::add_population(base_id, int(base_t.population * enemy_inf * 0.8));
					if (type) ch::add_mil_pop_all(base_id, int(base_t.population * enemy_inf * 0.2));

					// 兵临城下流民
					pk::list<pk::building @> neighbor_building_list;
					for (int neighbor_id = 0; neighbor_id < 城市_末; ++neighbor_id)
					{
						pk::building @building = pk::get_building(neighbor_id);
						if (neighbor_id == base_id)
							continue;

						int distance = pk::get_building_distance(neighbor_id, base_id);
						if (distance > 1)
							continue;

						if (!pk::is_valid_person_id(pk::get_kunshu_id(building)))
							continue;

						// 如果目标城市也在被攻击,则跳过.
						if (has_enemy(pk::get_building(neighbor_id)) != 0)
							continue;

						neighbor_building_list.add(building);
					}

					pop_gain_calc[building0.get_id()] += base_t.population * enemy_inf;

					if (neighbor_building_list.count > 1)
					{
						neighbor_building_list.sort(function(a, b) {
							pk::force @force_a = pk::get_force(a.get_force_id());
							pk::force @force_b = pk::get_force(b.get_force_id());

							pk::person @kunshu_a = pk::get_person(force_a.kunshu);
							pk::person @kunshu_b = pk::get_person(force_b.kunshu);

							return kunshu_a.stat[武将能力_魅力] > kunshu_b.stat[武将能力_魅力];
						});

						if (type) ch::add_population(neighbor_building_list[0].get_id(), int(base_t.population * -enemy_inf * 0.8));
						if (type) ch::add_population(neighbor_building_list[1].get_id(), int(base_t.population * -enemy_inf * 0.2));



						if (int(base_t.population * -enemy_inf) > 10000)
						{
							string t = pk::format("\x1b[2x{}\x1b[0x战火纷飞,人心惶惶,\x1b[2x{}\x1b[0x人口向\x1b[2x{}\x1b[0x和\x1b[2x{}\x1b[0x逃难", pk::decode(pk::get_name(building0)), int(base_t.population * -enemy_inf), pk::decode(pk::get_name(neighbor_building_list[0])), pk::decode(pk::get_name(neighbor_building_list[1])));

							pk::history_log(building0.pos, pk::get_force(building0.get_force_id()).color, pk::encode(t));
						}
					}
					// pk::trace(pk::format("{},魅力最高人口，{}", neighbor_building_list[0].get_id(), pk::decode(pk::get_name(neighbor_building_list[0])), neighbor_base_t.population));
				}

				//*******************************************  灾难流民  *********************************************************************************************

				if (疫病状态 or 蝗灾状态)
				{
					float eki_inf = 0.03f;
					if (type) ch::add_population(base_id, -int(base_t.population * eki_inf));
					if (type) ch::add_mil_pop_all(base_id, -int(base_t.population * eki_inf * 0.3));

					pk::list<pk::building @> neighbor_building_list;
					for (int neighbor_id = 0; neighbor_id < 城市_末; ++neighbor_id)
					{
						pk::building @building = pk::get_building(neighbor_id);
						if (neighbor_id == base_id)
							continue;

						int distance = pk::get_building_distance(neighbor_id, base_id);

						// 逃难距离只能临近
						if (distance > 2)
							continue;

						if (!pk::is_valid_person_id(pk::get_kunshu_id(building)))
							continue;

						// 如果目标城市也受到灾害则跳过.
						if (pk::building_to_city(building).ekibyou or pk::building_to_city(building).inago)
							continue;

						neighbor_building_list.add(building);
					}
					pop_gain_calc[building0.get_id()] += base_t.population * -eki_inf;

					if (neighbor_building_list.count > 1)
					{
						neighbor_building_list.sort(function(a, b) {
							pk::force @force_a = pk::get_force(a.get_force_id());
							pk::force @force_b = pk::get_force(b.get_force_id());

							pk::person @kunshu_a = pk::get_person(force_a.kunshu);
							pk::person @kunshu_b = pk::get_person(force_b.kunshu);

							return kunshu_a.stat[武将能力_魅力] > kunshu_b.stat[武将能力_魅力];
						});

						if (type) ch::add_population(neighbor_building_list[0].get_id(), int(base_t.population * eki_inf));
						if (type) ch::add_population(neighbor_building_list[1].get_id(), int(base_t.population * eki_inf * 0.3));



						if (int(base_t.population * 人口基础增长率 * 10.0f * 0.8) > 5000)
						{

							string t = pk::format("\x1b[2x{}\x1b[0x发生灾害,约\x1b[2x{}\x1b[0x人口向\x1b[2x{}\x1b[0x和\x1b[2x{}\x1b[0x逃难", pk::decode(pk::get_name(building0)), int(base_t.population * eki_inf), pk::decode(pk::get_name(neighbor_building_list[0])), pk::decode(pk::get_name(neighbor_building_list[1])));

							pk::history_log(building0.pos, pk::get_force(building0.get_force_id()).color, pk::encode(t));
						}
					}
					// pk::trace(pk::format("{},魅力最高人口，{}", neighbor_building_list[0].get_id(), pk::decode(pk::get_name(neighbor_building_list[0])), neighbor_base_t.population));
				}


			}
			//*******************************************  流民分配  *********************************************************************************************
			for (int province_id = 0; province_id < 州_末; ++province_id)
			{
				pk::list<pk::building @> province_base = ch::get_base_list(province_id);
				pk::list<pk::city @> province_city = pk::get_city_list(pk::get_province(province_id));
				// 大小城市权重不同.base.count也包括了城市,因此大城市计算两次,小城市计算1次,分配的时候也是按此比例
				// 比如说有2大2小四个据点,count=6,如果有1200/2=600人需要分配,600/6=100;大城市获得100*2,小城市获得100
				float count = province_city.count + province_base.count;
				float per_deserter = ch::get_set_p(0).province_deserter[province_id] * 0.10f / count; // 每月分配总量的10%

				//  if (province_id == 州_荆北)
				//  	pk::trace(pk::format("province_id：{},count：{},荆州总逃兵：{}", province_id, count,set_t.province_deserter[province_id]));

				for (int i = 0; i < province_base.count; ++i)
				{
					int base_id = province_base[i].get_id();
					auto building_p = ch::get_baseIA(base_id);
					if (base_id < 城市_末)
					{
						// 兵临城下据点无法分配
						if (pk::enemies_around(pk::get_building(base_id)))
							continue;

						//强征城市不分配
						if (building_p.building_policy == 0)
							continue;

							// 战斗状态的据点只有一半数量
						auto base_t = ch::get_base_p(base_id);
						int deserter_city = int(per_deserter * 2);
						if (ai::base_in_range_enemy_troops[base_id] > 0)
							deserter_city = 0;

						if (building_p.building_policy == 1)
							deserter_city /= 2;

						if (type) ch::get_set_p(0).province_deserter[province_id] -= uint32(deserter_city);
						// 大城市权重为2
						if (type) ch::add_population(base_id, uint32(deserter_city * 1.0f));
						// base_t.population += uint32(per_deserter*0.4f);
						if (type) ch::add_mil_pop_all(base_id, uint32(deserter_city * 0.0f));

						pop_gain_calc[i] += deserter_city;
						// if (调试模式 and base_id == 铃据点_江陵)
						// if (base_id == 铃据点_江陵)
						// 	pk::trace(pk::format("deserter,江陵人口：{},江陵兵役人口：{},江陵逃兵分配：{}", base_t.population, base_t.mil_pop_all, deserter_city));
					}
					else
					{
						// 兵临城下据点无法分配
						if (pk::enemies_around(pk::get_building(base_id)))
							continue;

						//强征城市不分配
						if (building_p.building_policy == 0)
							continue;

						auto base_t = ch::get_base_p(base_id);
						int deserter_base = int(per_deserter);
						if (ai::base_in_range_enemy_troops[base_id] > 0)
							deserter_base = 0;
						if (building_p.building_policy == 1)
							deserter_base /= 2;

						if (type) ch::get_set_p(0).province_deserter[province_id] -= uint32(deserter_base);
						// 大城市权重为1
						if (type) ch::add_population(base_id, uint32(deserter_base * 1.0f));
						if (type) ch::add_mil_pop_all(base_id, uint32(deserter_base * 0.0f));

						pop_gain_calc[i] += deserter_base;
						// if (调试模式 and base_id == 铃据点_麦城)
						// if (base_id == 铃据点_麦城)
						// 	pk::trace(pk::format("deserter,麦城人口：{},麦城兵役人口：{},麦城逃兵分配：{}", base_t.population, base_t.mil_pop_all, deserter_base));
					}
				}
			}

			//*******************************************  伤兵恢复  *********************************************************************************************
			// 伤兵恢复
			for (int base_id = 0; base_id < 据点_末; ++base_id)
			{
				auto base_t = ch::get_base_p(base_id);
				if (base_t.wounded > 0)
				{
					pk::building @building0 = pk::get_building(base_id);
					int heal_num = int(base_t.wounded * (pk::enemies_around(pk::get_building(base_id)) ? 0.0 : 0.5));
					if (ch::has_skill(building0, 特技_医者))
						heal_num = int(base_t.wounded * (pk::enemies_around(pk::get_building(base_id)) ? 0.5 : 0.8));
					if (调试模式)
						pk::trace(pk::format("伤兵恢复，wounded:{},heal_num：{}", base_t.wounded, heal_num));
					if (!pk::enemies_around(pk::get_building(base_id)))
						heal_num = pk::min(base_t.wounded, pk::max(500, heal_num)); // 设定伤兵最低恢复数
					if (type) base_t.wounded -= uint32(heal_num);
					if (type) ch::add_troops(building0, heal_num, true);
				}
			}
		}

		void onNewDay2()
		{
			if (!开启人口系统)
				return;

			// 发生两次攻破后会出现人口锐减且无法回归的问题,要改的话太麻烦了.累加的话又会导致人口波动特别大,先取消掉后面再想办法
			return_pop_conduct();

			set_interior_land();
		}

		void return_pop_conduct()
		{
			if (!开启人口系统)
				return;

			//返回人口的执行
			//首先对回归期限进行处理，确认是否产生回归buf
			for (int base_id = 0; base_id < 据点_末; ++base_id)
			{
				auto base_t = ch::get_base_p(base_id);
				pk::building @building = pk::get_building(base_id);

				if (base_t.return_timer > 0)//破城后的回归期限
				{
					if (base_t.return_buf_timer == 0)
					{
						int public_order = ch::get_public_order(base_id);
						if (public_order > 89)
						{
							if (base_id >= 城市_末 or pk::get_city(base_id).num_devs > (building.is_player() ? 9 : 2))
							{
								base_t.return_timer = 0; // 破城后的回归期限清空
								base_t.return_buf_timer = 5;
							}
						}
					}
					base_t.return_timer -= 1;
				}

				if (base_t.return_buf_timer > 0)
				{
					// if (base_id == 城市_襄阳) pk::trace(pk::format("onNewDay2 return_buf,襄阳人口：{}", base_t.population));
					base_t.return_pop -= uint32(base_t.return_pop * 0.3f);
					// base_t.population = uint32(temp);
					ch::add_population(base_id, uint32(base_t.return_pop * 0.3f));
					base_t.return_buf_timer -= 1;//对应buf时间-1
				}
			}
		}

		void onUnitRemove(pk::unit @unit, pk::hex_object @dst, int type)
		{
			//pk::trace(pk::format("onUnitRemove0", 1));
			if (dst is null)
				return;
			if (type == 1)
			{
				pk::building @dst_building = dst.get_type_id() == pk::building::type_id ? dst : null; // 将hex_object转为建筑指针
				//pk::trace(pk::format("onUnitRemove0,id:{}", dst_building.get_id()));
				if (!pk::is_alive(dst_building))
					return;
				//pk::trace(pk::format("onUnitRemove1", 1));
				int base_id = dst_building.get_id();
				int unit_id = unit.get_id();

				auto base_t = ch::get_base_p(base_id);
				auto unit_t = ch::get_unit_p(unit_id);

				// pk::info("unit:" + (unit_t.wounded));

				base_t.wounded += pk::max(0, unit_t.wounded);
				//if (base_id == 城市_襄阳 or base_id == 城市_江陵) pk::trace(pk::format("onUnitRemove,伤兵：{},{}", base_t.wounded, unit_t.wounded));
				unit_t.wounded = 0;
			}
			if (type == 0)
			{
				// pk::trace("remove type 0");
				pk::building @dst_building = dst.get_type_id() == pk::building::type_id ? dst : null;
				pk::unit @dst_unit = dst.get_type_id() == pk::unit::type_id ? dst : null;
				// 分为部队击破部队，据点击破部队，其他建筑击破部队，同时需区分是否友军,（区分是否异族势力）
				if (dst_unit !is null)
				{
					// unit 是被灭方
					// pk::trace(pk::format("{}击破{}",pk::decode(pk::get_name(dst_unit)), pk::decode(pk::get_name(unit))));
					auto unit_t = ch::get_unit_p(unit.get_id());
					int unit_wounded = unit_t.wounded;
					unit_t.wounded = 0;

					// pk::trace(pk::format("{}击破{}, 伤兵：{}", pk::decode(pk::get_name(dst_unit)), pk::decode(pk::get_name(unit)), unit_wounded));
					if (pk::is_enemy(unit, dst_unit))
					{

						unitinfo dst_t(dst_unit.get_id());
						dst_t.wounded += uint16(0.5f * unit_wounded);
						dst_t.update(dst_unit.get_id());
						int unit_service = pk::get_service(unit);
						if (unit_service > -1 and unit_service < 据点_末)
						{
							auto base_t = ch::get_base_p(unit_service);
							base_t.wounded += uint16(0.5f * unit_wounded);
						}
					}
					else
					{
						int unit_service = pk::get_service(unit);
						if (pk::is_valid_base_id(unit_service))
						{
							auto base_t = ch::get_base_p(unit_service);
							base_t.wounded += uint16(unit_wounded);
						}
					}
				}
				if (dst_building !is null)
				{
					// unit 是被灭方
					auto unit_t = ch::get_unit_p(unit.get_id());
					int unit_wounded = unit_t.wounded;
					unit_t.wounded = 0;

					if (pk::is_enemy(unit, dst_building))
					{
						int dst_building_id = dst_building.get_id();
						if (pk::is_valid_base_id(dst_building_id))
						{
							auto dst_t = ch::get_base_p(dst_building.get_id());
							dst_t.wounded += uint16(0.5f * unit_wounded);
						}
						int unit_service = pk::get_service(unit);
						if (pk::is_valid_base_id(unit_service))
						{
							auto base_t = ch::get_base_p(unit_service);
							base_t.wounded += uint16(0.5f * unit_wounded);
						}
					}
					else
					{
						int unit_service = pk::get_service(unit);
						if (pk::is_valid_base_id(unit_service))
						{
							auto base_t = ch::get_base_p(unit_service);
							base_t.wounded += uint16(unit_wounded);
						}
					}
				}
			}
		}

		int has_enemy(pk::building @base)
		{
			// 1格内敌部队数
			bool has_enemy_units1 = false;
			// 3格内敌部队数
			bool has_enemy_units3 = false;

			auto range = pk::range(base.get_pos(), 1, 3 + (base.facility == 设施_都市 ? 1 : 0));
			for (int i = 0; i < int(range.length); i++)
			{
				auto unit = pk::get_unit(range[i]);
				if (pk::is_alive(unit))
				{
					int distance = pk::get_distance(base.get_pos(), range[i]);
					if (pk::is_enemy(base, unit))
					{
						if (distance <= 1)
						{
							has_enemy_units1 = true;
							break;
						}
						if (distance <= 3)
						{
							has_enemy_units3 = true;
						}
					}
					else
					{
					}
				}
			}

			if (has_enemy_units1)
				return 2;
			if (has_enemy_units3)
				return 1;
			return 0;
		}

		// 可以通过返回值判断是几级兵舍
		float func_5c4600(pk::city @city)
		{
			int level1 = 0, level2 = 0;
			for (int i = 0; i < int(city.max_devs); i++)
			{
				pk::building @building = city.dev[i].building;
				if (pk::is_alive(building))
				{
					switch (building.facility)
					{
						case 시설_병영:
							building.completed ? level1++ : 0;
							break;
						case 시설_병영2단:
							building.completed ? level2++ : level1++;
							break;
						case 시설_병영3단:
							building.completed ? 0 : level2++;
							break;
					}
				}
			}
			if (int(city.barracks_counter) > level1 + level2)
				return 1.5f;
			if (int(city.barracks_counter) > level1)
				return 1.2f;
			if (level1 > 0)
				return 1.f;
			return 0.f;
		}

		int get_city_revenue(int city_id)
		{
			return cast<pk::func150_t>(pk::get_func(150))(pk::get_city(city_id));
		}

		void set_interior_land()
		{
			// 根据人口规模调整内政用地
			for (int city_id = 0; city_id < 城市_末; ++city_id)
			{
				ch::set_interior_land(city_id);
			}
		}
		float difficulty_when_start(int building_id)
		{

			float 难度修正 = 1.0;
			if (!pk::get_building(building_id).is_player())
			{
				if (pk::get_scenario().difficulty == 难易度_超级)
					难度修正 = 1.5;
				if (pk::get_scenario().difficulty == 难易度_上级)
					难度修正 = 1.2;
				if (pk::get_scenario().difficulty == 难易度_初级)
					难度修正 = 1.0;
			}
			return 难度修正;
		}

	} // class Main

	Main main;

	// 人口数据格式为: PK2.2地名 - 新地图地名{总人口,兵役人口,预备兵}
	// 由于原始数据中方差太大,考虑游戏性和平衡性,此数据对人口过多和过少的城市进行了修正,所有关隘(虎牢关)和战争地名(赤壁)均保留30000,
	// 总人口3000万,大致符合东汉末年黄巾起义之前人口设定
	// 采用基准*系数的方式,便于调整全局人口
	// 原剧本人口中数值甩动太大,出现大量的人口极多和极缺的情况,均导致游戏初期体验太差,保留火烧洛阳的剧本情况下暂时统一剧本人口比例.未来根据情况再调整
	// 襄平北平,荆南四郡.平原.永安.梓潼.建宁.初始为小城.

	const array<array<int>> 大城剧本人口 = {
		/*辽东 */ {415267, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*北平 */ {428558, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*蓟 */ {1004986, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*渤海 */ {1630743, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*平原 */ {609627, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*太原 */ {741278, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*邺 */ {1417155, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*北海 */ {847358, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*广陵 */ {866374, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*下邳 */ {880428, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*寿春 */ {925695, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*濮阳 */ {892188, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*陈留 */ {773306, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*许昌 */ {1023259, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*汝南 */ {1265562, 100, 112, 110, 109, 109, 118, 137, 097, 110, 109, 124, 101, 112, 097, 0, 103},
		/*洛阳 */ {1463681, 100, 114, 005, 054, 054, 074, 085, 096, 027, 054, 078, 117, 114, 096, 0, 103},
		/*宛 */ {1319418, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*长安 */ {952813, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*朔方 */ {366957, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*安定 */ {339238, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*天水 */ {369396, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*武威 */ {375517, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*建业 */ {1141278, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*吴 */ {802843, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*建安 */ {749336, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*庐江 */ {820991, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*柴桑 */ {710761, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*江夏 */ {387599, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*云中 */ {329712, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*襄阳 */ {1057839, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*江陵 */ {714849, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*长沙 */ {656430, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*武陵 */ {458972, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*河东 */ {883182, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*合浦 */ {828634, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*巴东 */ {449627, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*汉中 */ {621821, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*梓潼 */ {454424, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*巴郡 */ {917696, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*成都 */ {1454338, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*南海 */ {720108, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},
		/*云南 */ {852305, 100, 101, 109, 107, 107, 105, 106, 103, 109, 107, 109, 110, 101, 103, 0, 103},

	};


	const array<array<int>> 小城剧本人口 = {
		/*,42壶关,*/ {30000, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100},
		/*,43虎牢关,*/ {30000, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100},
		/*,44潼关,*/ {30000, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100},
		/*,45函谷关,*/ {30000, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100},
		/*,46寰辕关,*/ {30000, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100},
		/*,47阳平关,*/ {30000, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100},
		/*,48犍为,*/ {230000, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100},
		/*,49剑门关,*/ {30000, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100},
		/*,50郁林,*/ {110000, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100},
		/*,51绵竹关,*/ {30000, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100},
		/*,52昌黎,*/ {114701, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,53涿郡,*/ {180831, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,54会稽,*/ {164424, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,55雁门,*/ {170731, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,56泰山,*/ {288468, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,57济北,*/ {238468, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,58淮阴,*/ {194022, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,59济阴,*/ {217209, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,60彭城,*/ {225979, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,61谯郡,*/ {268603, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,62荥阳,*/ {235314, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,63上洛,*/ {152512, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,64琅琊,*/ {318799, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,65扶风,*/ {145663, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,66河内,*/ {276951, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,67上庸,*/ {215435, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,68合肥,*/ {83355, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,69宣城,*/ {188483, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,70桂阳,*/ {281862, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,71乐浪,*/ {398551, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,72鄱阳,*/ {161819, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,73定襄,*/ {153996, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,74豫章,*/ {235300, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100},
		/*,75信都,*/ {265622, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,76武都,*/ {77151, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,77弘农,*/ {172786, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,78上郡,*/ {106851, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,79零陵,*/ {310638, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,80夷陵,*/ {152525, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,81新野,*/ {256321, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,82当阳,*/ {133355, 100, 101, 119, 111, 111, 114, 111, 113, 105, 099, 110, 122, 101, 1012, 000, 103},
		/*,83越巂,*/ {152522, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100},
		/*,84金城,*/ {155278, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,85建宁,*/ {332000, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103},
		/*,86巴西,*/ {260631, 100, 101, 109, 100, 100, 100, 100, 100, 100, 100, 100, 110, 101, 133, 000, 103} };

	const array<array<int>> 史实模式人口 = {
		/*0襄平-辽东*/ {380574, 38952, 16317},
		/*1北平-北平*/ {639299, 63255, 17751},
		/*2蓟-蓟*/ {1281089, 136787, 15595},
		/*3南皮-中山*/ {652491, 83610, 17870},
		/*4平原-渤海*/ {1126165, 129062, 19687},
		/*5晋阳-晋阳*/ {231983, 32664, 14888},
		/*6邺-邺*/ {897242, 64138, 18046},
		/*7北海-北海*/ {855777, 96197, 18732},
		/*8广陵-下邳*/ {616271, 72919, 17639},
		/*9下邳-彭城*/ {499298, 51033, 17011},
		/*10寿春-淮南*/ {638874, 63208, 17736},
		/*11濮阳-濮阳*/ {605229, 62803, 17601},
		/*12陈留-陈留*/ {867516, 86390, 18796},
		/*13许昌-许昌*/ {1430351, 132261, 20753},
		/*14汝南-汝南*/ {2108028, 237558, 22519},
		/*15洛阳-洛阳*/ {1022272, 118030, 19343},
		/*16宛-南阳*/ {2438863, 239873, 23291},
		/*17长安-长安*/ {582155, 56906, 15635},
		/*18上庸-广陵*/ {415870, 59541, 16513},
		/*19安定-安定*/ {59283, 26587, 2195},
		/*20天水-陇西*/ {61479, 26831, 2277},
		/*21武威-武威*/ {65124, 27236, 2412},
		/*22建业-建业*/ {638874, 73208, 17736},
		/*23吴-吴*/ {707887, 74209, 18069},
		/*24会稽-建安*/ {863632, 88181, 16060},
		/*25庐江-庐江*/ {428330, 49814, 16604},
		/*26柴桑-柴桑*/ {1662408, 161378, 27126},
		/*27江夏-江夏*/ {267775, 36419, 15473},
		/*28新野-河东*/ {800725, 82302, 27434},
		/*29襄阳-襄阳*/ {531254, 55694, 18564},
		/*30江陵-江陵*/ {723598, 84844, 18281},
		/*31长沙-长沙*/ {1057051, 128561, 19520},
		/*32武陵-武陵*/ {254480, 26053, 15351},
		/*33桂阳-云中*/ {268727, 26525, 18175},
		/*34零陵-朔方*/ {78530, 19836, 5945},
		/*35永安-巴东*/ {365226, 28358, 16119},
		/*36汉中-汉中*/ {267206, 26467, 15489},
		/*37梓潼-梓潼*/ {501794, 51310, 17103},
		/*38江州-巴郡*/ {1089620, 118846, 19615},
		/*39成都-成都*/ {1353266, 131474, 20491},
		/*40建宁-南中*/ {894532, 86059, 12019},
		/*41云南-合浦*/ {864415, 38490, 13496},
		/*42壶关-上党*/ {127403, 11666, 6555},
		/*43虎牢关-虎牢关*/ {30000, 1666, 555},
		/*44潼关-潼关*/ {30000, 1666, 555},
		/*45函谷关-函谷关*/ {30000, 1666, 555},
		/*46武关-上洛*/ {30000, 1666, 555},
		/*47阳平关-阳平关*/ {30000, 1666, 555},
		/*48大散关-北地*/ {52530, 5836, 1945},
		/*49葭萌关-剑阁*/ {30000, 1666, 555},
		/*50涪水关-阆中*/ {30000, 1666, 555},
		/*51绵竹关-绵竹*/ {30000, 1666, 555},
		/*52柳城-昌黎*/ {430173, 50019, 16673},
		/*53代-平原*/ {491455, 57939, 9313},
		/*54山越-黎阳*/ {552093, 53565, 17855},
		/*55上党-常山*/ {508959, 53217, 17739},
		/*56东平-城阳*/ {453044, 50338, 16779},
		/*57任城-雁门*/ {57342, 6371, 2123},
		/*58淮阴-零陵*/ {551347, 67927, 19309},
		/*59堂邑-桂阳*/ {520578, 61175, 17058},
		/*60小沛-濡须*/ {60012, 6668, 2222},
		/*61谯-济北*/ {235957, 35661, 5220},
		/*62官渡-上郡*/ {28609, 6734, 2244},
		/*63上洛-轘辕关*/ {30000, 1666, 555},
		/*64乌桓-河内*/ {829918, 25546, 8515},
		/*65陈仓-苍梧*/ {465232, 20581, 6860},
		/*66鲜卑-上谷*/ {60609, 6734, 2244},
		/*67西城-上庸*/ {464843, 50538, 6846},
		/*68合肥-定襄*/ {52530, 5836, 1945},
		/*69宛陵-五原*/ {55509, 6167, 2055},
		/*70考城-曲阿*/ {362798, 36977, 2325},
		/*71白水-会稽*/ {487468, 50829, 6943},
		/*72查读-临淮*/ {493188, 49243, 3081},
		/*73建安-豫章*/ {668299, 74255, 11418},
		/*74赤壁-赤壁*/ {30000, 1666, 555},
		/*75南蛮-鄱阳*/ {208874, 23208, 7736},
		/*76西羌-谯*/ {674762, 60529, 10176},
		/*77永昌-弋阳*/ {162798, 16977, 2325},
		/*78北地-新野*/ {128568, 16507, 2169},
		/*79樊城-樊城*/ {114445, 16049, 2016},
		/*80夷陵-华容*/ {30000, 1666, 555},
		/*81新野-临川*/ {325357, 27039, 2346},
		/*82麦城-金城*/ {52530, 5836, 1945},
		/*83越巂-陈仓*/ {30000, 1666, 555},
		/*84街亭-朱提*/ {37674, 6408, 2136},
		/*85雒城-临沅*/ {251466, 6829, 2276},
		/*86阆中-夷陵*/ {62895, 6988, 2329}

	};

	// 人口数据格式为: PK2.2地名 - 新地图地名{总人口,兵役人口,预备兵}
	// 进一步修正了城市间差异,所有关隘(虎牢关)和战争地名(赤壁)均保留30000,
	// 总人口3500万,大致符合东汉末年黄巾起义之后人口设定

	const array<array<int>> 虚构模式人口 = {
		/*0襄平-辽东*/ {493525, 49341, 25548},
		/*1北平-北平*/ {539649, 62876, 26647},
		/*2蓟-蓟*/ {524143, 47950, 24976},
		/*3南皮-中山*/ {346215, 72288, 26736},
		/*4平原-渤海*/ {848967, 89813, 28062},
		/*5晋阳-晋阳*/ {385317, 12903, 24403},
		/*6邺-邺*/ {968008, 63314, 26867},
		/*7北海-北海*/ {640066, 77539, 27373},
		/*8广陵-下邳*/ {628023, 67509, 26562},
		/*9下邳-彭城*/ {565288, 56476, 26085},
		/*10寿春-淮南*/ {639437, 62853, 26635},
		/*11濮阳-濮阳*/ {822372, 62651, 26534},
		/*12陈留-陈留*/ {1045124, 73480, 27420},
		/*13许昌-许昌*/ {1156778, 90919, 28812},
		/*14汝南-汝南*/ {561524, 121850, 30013},
		/*15洛阳-洛阳*/ {808860, 85889, 27816},
		/*16宛-南阳*/ {1249349, 122442, 30523},
		/*17长安-长安*/ {10610393, 109637, 25008},
		/*18上庸-广陵*/ {515904, 61003, 25701},
		/*19安定-安定*/ {194785, 20290, 9370},
		/*20天水-陇西*/ {198360, 20662, 9544},
		/*21武威-武威*/ {204155, 21266, 9822},
		/*22建业-建业*/ {1123437, 127642, 26635},
		/*23吴-吴*/ {673088, 68103, 26884},
		/*24会稽-建安*/ {743454, 74238, 25346},
		/*25庐江-庐江*/ {523575, 55798, 25771},
		/*26柴桑-柴桑*/ {931475, 100430, 32940},
		/*27江夏-江夏*/ {313976, 47709, 24878},
		/*28新野-河东*/ {515866, 71721, 33126},
		/*29襄阳-襄阳*/ {983097, 58999, 27250},
		/*30江陵-江陵*/ {880517, 72820, 27041},
		/*31长沙-长沙*/ {822504, 89639, 27943},
		/*32武陵-武陵*/ {403568, 40352, 24780},
		/*33桂阳-云中*/ {214711, 40716, 26963},
		/*34零陵-朔方*/ {224186, 24794, 15421},
		/*35永安-巴东*/ {325184, 33873, 25392},
		/*36汉中-汉中*/ {313536, 40672, 24891},
		/*37梓潼-梓潼*/ {566699, 56629, 26156},
		/*38江州-巴郡*/ {635079, 86185, 28011},
		/*39成都-成都*/ {1230640, 90648, 28629},
		/*40建宁-南中*/ {576638, 73340, 21926},
		/*41云南-合浦*/ {743791, 49047, 23234},
		/*42壶关-上党*/ {185548, 27002, 16193},
		/*43虎牢关-虎牢关*/ {30000, 1666, 555},
		/*44潼关-潼关*/ {30000, 1666, 555},
		/*45函谷关-函谷关*/ {30000, 1666, 555},
		/*46武关-上洛*/ {30000, 1666, 555},
		/*47阳平关-阳平关*/ {30000, 1666, 555},
		/*48大散关-北地*/ {93355, 19098, 8820},
		/*49葭萌关-剑阁*/ {97980, 10204, 4712},
		/*50涪水关-阆中*/ {97980, 10204, 4712},
		/*51绵竹关-绵竹*/ {97980, 10204, 4712},
		/*52柳城-昌黎*/ {154701, 55912, 25825},
		/*53代-平原*/ {180831, 60176, 19301},
		/*54山越-黎阳*/ {164424, 57860, 26725},
		/*55上党-常山*/ {170731, 57672, 26638},
		/*56东平-城阳*/ {138468, 56090, 25907},
		/*57任城-雁门*/ {191570, 19955, 9215},
		/*58淮阴-零陵*/ {194022, 65157, 27791},
		/*59堂邑-桂阳*/ {177209, 61834, 26121},
		/*60小沛-濡须*/ {195979, 20414, 9428},
		/*61谯-济北*/ {188603, 47210, 14450},
		/*62官渡-上郡*/ {135314, 20515, 9474},
		/*63上洛-轘辕关*/ {30000, 1666, 555},
		/*64乌桓-河内*/ {128799, 39958, 18455},
		/*65陈仓-苍梧*/ {145663, 35865, 16565},
		/*66鲜卑-上谷*/ {196951, 20515, 9474},
		/*67西城-上庸*/ {245435, 56202, 16548},
		/*68合肥-定襄*/ {183355, 19098, 8820},
		/*69宛陵-五原*/ {188483, 19633, 9066},
		/*70考城-曲阿*/ {181862, 48074, 9644},
		/*71白水-会稽*/ {158551, 56363, 16665},
		/*72查读-临淮*/ {161819, 55477, 11101},
		/*73建安-豫章*/ {253996, 68124, 21371},
		/*74赤壁-赤壁*/ {30000, 1666, 555},
		/*75南蛮-鄱阳*/ {165622, 38085, 17591},
		/*76西羌-谯*/ {217151, 61507, 20175},
		/*77永昌-弋阳*/ {122786, 32574, 9644},
		/*78北地-新野*/ {186851, 32120, 9315},
		/*79樊城-樊城*/ {170638, 31671, 8980},
		/*80夷陵-华容*/ {30000, 1666, 555},
		/*81新野-临川*/ {156321, 41109, 9687},
		/*82麦城-金城*/ {183355, 19098, 8820},
		/*83越巂-陈仓*/ {30000, 1666, 555},
		/*84街亭-朱提*/ {155278, 20012, 9243},
		/*85雒城-临沅*/ {221171, 20659, 9541},
		/*86阆中-夷陵*/ {200631, 20899, 9652},

	};
}
